package com.yhp.web;

import com.yhp.enums.YesOrNo;
import com.yhp.pojo.Carousel;
import com.yhp.pojo.Category;
import com.yhp.service.CarouselService;
import com.yhp.service.CategoryService;
import com.yhp.utils.JSONResult;
import com.yhp.utils.JsonUtils;
import com.yhp.utils.RedisOperator;
import com.yhp.vo.CategoryVO;
import com.yhp.vo.ItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(value = "首页",tags = {"展示首页数据的接口"})
@RequestMapping("index")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisOperator redisOperator;


    //轮播图
    @GetMapping("/carousel")
    @ApiOperation(value = "获得首页轮播图信息列表",notes = "获得首页轮播图信息列表",httpMethod = "GET")
    public JSONResult queryAll(){

        List carouselList=new ArrayList();
        String carousel = redisOperator.get("carousel");
        if(StringUtils.isBlank(carousel)){
            carouselList = carouselService.queryAll(YesOrNo.YES.type);
            redisOperator.set("carousel",JsonUtils.objectToJson(carouselList));
        }else{
            carouselList=JsonUtils.jsonToList(carousel,Carousel.class);
        }
        //更新轮播图内容的思路
        //1.使用后台运营系统，一旦广告(轮播图)发生更改，就可以删除缓存，然后重置
        //2.定时重置，比如每天凌晨三点重置
        //3.每个轮播图都有可能是一个广告，每个广告都会有一个过期时间，过期了，再重置
        return JSONResult.ok(carouselList);
    }

    /*
    首页分类展示需求:
      1.第一次刷新首页时，展示大分类，并渲染到首页
      2.当鼠标放在大分类的文字上时，再去加载子分类的内容，如果前端已存在子分类，
         则不需要再加载(懒加载 )
     */
    @GetMapping("/cats")
    @ApiOperation(value = "查询大分类",notes = "查询大分类",httpMethod = "GET")
    public JSONResult queryAllRootCats(){
        List categoriesList=new ArrayList();
        String category = redisOperator.get("cats");
        if(StringUtils.isBlank(category)){
            categoriesList = categoryService.queryAllRootCats();
            redisOperator.set("cats",JsonUtils.objectToJson(categoriesList));
        }else{
            categoriesList=JsonUtils.jsonToList(category,Category.class);
        }

        return JSONResult.ok(categoriesList);
    }
    //查询子分类
    @GetMapping("/subCat/{typeid}")
    @ApiOperation(value = "查询子分类",notes = "查询子分类",httpMethod = "GET")
    public JSONResult queryAllSecondCats(
            @ApiParam(name = "typeid",value = "一级分类id",required = true)
            @PathVariable("typeid")Integer typeid){
        if(typeid==null){
            return JSONResult.errorMsg("类型id不允许为null");
        }
       // List<CategoryVO> categoryVOS = categoryService.querySecondCats(typeid);
        List<CategoryVO> categoryVOS =new ArrayList<>();
        String catsStr = redisOperator.get("subCat:" + typeid);
        if(StringUtils.isBlank(catsStr)){
           categoryVOS = categoryService.querySecondCats(typeid);
           if(categoryVOS!=null&&categoryVOS.size()>0){
               redisOperator.set("subCat:" + typeid,JsonUtils.objectToJson(categoryVOS));
           }else{
               redisOperator.set("subCat:" + typeid,JsonUtils.objectToJson(categoryVOS),5*60);
           }
        }else{
            categoryVOS = JsonUtils.jsonToList(catsStr, CategoryVO.class);
        }

        return JSONResult.ok(categoryVOS);
    }

    //查询六个推荐
    @GetMapping("/sixNewItems/{rootId}")
    @ApiOperation(value = "查询六个推荐",notes = "查询六个推荐",httpMethod = "GET")
    public JSONResult querySix(
            @ApiParam(name = "rootId",value = "一级分类id",required = true)
            @PathVariable("rootId")Integer rootId){
        if(rootId==null){

            return JSONResult.errorMsg("类型id不允许为null");
        }
        List<ItemVO> itemVOS = categoryService.querySix(rootId);
        return JSONResult.ok(itemVOS);
    }

}
